﻿using System;
using System.Security.Cryptography;

namespace Framework.Crypto.Hashers
{
    /// <summary>
    /// Md5 hash algorythm.
    /// </summary>
    public class Md5Hasher : IHasher
    {
        /// <summary>
        /// Md5 Crypto loaded
        /// </summary>
        private MD5 md5M;

        /// <summary>
        /// Generic Md5 constructor with some byte array to sign.
        /// </summary>
        /// <param name="signer"></param>
        public Md5Hasher(byte[] signer)
            : base(signer)
        {
            md5M = MD5.Create();
        }

        /// <summary>
        /// Returns computed by Md5 hash byte array.
        /// </summary>
        /// <returns>signed byte[]</returns>
        public override byte[] Sign()
        {
            return md5M.ComputeHash(signer);
        }

        /// <summary>
        /// Returns a string-based hash representation.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return BitConverter.ToString(Sign()).Replace("-", "");
        }

        /// <summary>
        /// Clears byte buffer.
        /// </summary>
        public override void Clear()
        {
            signer = new byte[0];
            md5M.Clear();
        }
    }
}
